package com.bringway.p2p.controller;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.bringway.framework.controller.JsonController;
import com.bringway.framework.util.DateUtils;
import com.bringway.framework.util.RequestUtils;
import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.service.VersionService;
/**
 * 
 * @author qjy
 * @version 创建时间：2016年5月8日 上午9:34:42
 * @功能：版本管理
 */
@Controller
@RequestMapping("/version")
public class VersionController  extends JsonController {
	
	@Autowired
	private VersionService versionService;
	/**
	 * 
	 * @param request
	 * @param response 查询数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryVersion")
	public ModelAndView queryVersion(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		JSONObject params = new JSONObject();
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String version= RequestUtils.getStringParameter(request, "version");
		String versionname= RequestUtils.getStringParameter(request, "versionname");
		String sblx= RequestUtils.getStringParameter(request, "sblx");
		String sjlx= RequestUtils.getStringParameter(request, "sjlx");
	
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("version", version);
		params.put("versionname", versionname);
		params.put("sblx", sblx);
		params.put("sjlx", sjlx);

		
		List<Map<String, Object>> list = versionService.queryVersionList(params);
		int count = versionService.getVersionCount(params);
		result.put("data", list);	
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request,response,result);
		return null;
	}
	/**
	 * @方法名称:updateVersion
	 * @方法描述:更新版本信息
	 * @author qjy
	 * @date 2017年5月8日10:36:13
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateVersion")
	public ModelAndView updateVersion(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = RequestUtils.getStringParameter(request, "id");
		String versionname = RequestUtils.getStringParameter(request, "versionname");
		String dload = RequestUtils.getStringParameter(request, "dload");
		String content = RequestUtils.getStringParameter(request, "content");
		String sjlx = RequestUtils.getStringParameter(request, "sjlx");
		String sblx = RequestUtils.getStringParameter(request, "sblx");
		JSONObject params = new JSONObject();
		params.put("id", id);
		params.put("versionname", versionname);
		params.put("dload", dload);
		params.put("content", content);
		params.put("sjlx", sjlx);
		params.put("sblx", sblx);
		JSONObject result = new JSONObject();
		boolean bool = versionService.updateVersion(params);
		result.put("success", true);
		result.put("isUpdate", bool);
		serialize(request, response, result);
		return null;
	}
	/**
	 * @方法名称:addVersion
	 * @方法描述:添加新版本信息
	 * @author qjy
	 * @date 2017年5月8日10:36:13
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/addVersion")
	public ModelAndView addVersion(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String version = RequestUtils.getStringParameter(request, "version");
		String versionname = RequestUtils.getStringParameter(request, "versionname");
		String sblx = RequestUtils.getStringParameter(request, "sblx");
		String dload = RequestUtils.getStringParameter(request, "dload");
		String content = RequestUtils.getStringParameter(request, "content");
		String sjlx = RequestUtils.getStringParameter(request, "sjlx");
		JSONObject params = new JSONObject();
		params.put("version", version);
		params.put("versionname", versionname);
		params.put("sblx", sblx);
		params.put("dload", dload);
		params.put("content", content);
		params.put("sjlx", sjlx);
		boolean bool = versionService.addVersion(params);
		JSONObject result = new JSONObject();
		result.put("success", true);
		result.put("isAdd", bool);
		serialize(request, response, result);
		return null;
	}
}
